package io.polly.ldz.controller;

import io.polly.ldz.model.RestResponse;
import io.polly.ldz.model.SystemCode;
import io.polly.ldz.model.entity.User;
import io.polly.ldz.model.vm.LoginRequestVM;
import io.polly.ldz.model.vm.TokenVM;
import io.polly.ldz.model.vm.UserResponseVM;
import io.polly.ldz.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author ldz112@163.com
 * @Classname AuthController
 * @Version V1.0
 * @Description TODO
 * @Date 2025/5/27 11:17
 * Copyright (C) 2023 All Rights Reserved
 */
@RestController
@RequestMapping(value = "/v1/user")
public class AuthController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RestResponse<UserResponseVM> select(@PathVariable Integer id) {

        if(id==1) {
            UserResponseVM userVm = new UserResponseVM();
            userVm.setId(id);
            userVm.setUserName("admin");
            return RestResponse.ok(userVm);
        }
        else  {
            return new RestResponse<>(SystemCode.InnerError);
        }

    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public RestResponse<String> register() {
        return  RestResponse.ok("hello");
    }


    @PostMapping("/login")
    public RestResponse<TokenVM> login(@RequestBody @Valid LoginRequestVM requestVM){
        User user = new User();
        user.setId(1);
        user.setUserName("admin");

        TokenVM tokenVM = new TokenVM();
        tokenVM.setUserID(user.getId());
        tokenVM.setUserName(user.getUserName());
        tokenVM.setAccessToken(jwtTokenUtil.generateToken(user));
        return RestResponse.ok(tokenVM);

    }


}
